import QtQuick 2.1
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0
import QtQuick.Dialogs 1.0
import QtQuick.Window 2.1
import Graphics 1.0

ApplicationWindow {
    width: 800
    height: 600
    minimumWidth: 400
    minimumHeight: 300

    title: "Quince System"

    Action {
        id: fileNewAction
        shortcut: "Ctrl+N"
        iconSource: "images/filenew.png"
        text: "&New"
        tooltip: "Create a new file"
    }

    Action {
        id: fileOpenAction
        shortcut: "Ctrl+O"
        iconSource: "images/fileopen.png"
        text: "&Open"
        tooltip: "Open an existing file"
    }

    Action {
        id: fileSaveAction
        shortcut: "Ctrl+S"
        iconSource: "images/filesave.png"
        text: "&Save"
        tooltip: "Save the document to disk"
    }

    Action {
        id: fileSaveAsAction
        shortcut: "Ctrl+A"
        iconSource: "images/exportpdf.png"
        text: "Save &As..."
        tooltip: "Save the document under a new name"
    }

    Action {
        id: fileSaveAsXML
        iconSource: "images/exportpdf.png"
        text: "Save As XML..."
        tooltip: "Save the document as XML document"
    }

    Action {
        id: cutAction
        text: "Cut"
        shortcut: "ctrl+x"
        iconSource: "images/editcut.png"
    }

    Action {
        id: copyAction
        text: "Copy"
        shortcut: "Ctrl+C"
        tooltip: "Copy the current selection's contents to the clipboard"
        iconSource: "images/editcopy.png"
    }

    Action {
        id: pasteAction
        text: "Paste"
        shortcut: "Ctrl+v"
        tooltip: "Paste the clipboard's content into the current selection"
        iconSource: "images/editpaste.png"
        iconName: "edit-paste"
    }

    Action {
        id: selectAction
        text: "Select"
        onTriggered: scene.shape = ShapeType.None
        iconSource: "images/tool_arrow.png"
    }

    Action {
        id: zoomAction
        text: "Zoom"
        onTriggered: scene.shape = ShapeType.Zoom
        iconSource: "images/tool_zoom.png"
    }
    Action {
        id: createSegmentAction
        text: "Create Segment"
        onTriggered: scene.shape = ShapeType.Line
        iconSource: "images/tool_segment.png"
    }

    Action {
        id: createRectangleAction
        text: "Create Rectangle"
        onTriggered: scene.shape = ShapeType.Rect
        iconSource: "images/tool_rectangle.png"
    }

    Action {
        id: createPolygonAction
        text: "Create Polygon"
        iconSource: "images/tool_polygon.png"
    }

    Action {
        id: createPointAction
        text: "Create Point"
        onTriggered: scene.shape = ShapeType.Point
        iconSource: "images/tool_point.png"
    }

    Action {
        id: createCurveAction
        text: "Create Curve"
        onTriggered: scene.shape = ShapeType.Bezier
        iconSource: "images/tool_curve.png"
    }

    Action {
        id: createCircleAction
        text: "Create Circle"
        onTriggered: scene.shape = ShapeType.Circle
        iconSource: "images/tool_circle.png"
    }

    Action {
        id: createArcAction
        text: "Create Arc"
        iconSource: "images/tool_ellipse.png"
    }

    menuBar: MenuBar {
        Menu {
            id: fileMenu
            title: "&File"
            MenuItem { action: fileNewAction }
            MenuItem { action: fileOpenAction }
            MenuItem { action: fileSaveAction }
            MenuItem { action: fileSaveAsAction }
            MenuItem { action: fileSaveAsXML }
            MenuSeparator {}
            MenuItem { text: "Quit"; onTriggered: Qt.quit() }
        }
        Menu {
            id: editMenu
            title: "&Edit"
            MenuItem { action: copyAction }
            MenuItem { action: cutAction }
            MenuItem { action: pasteAction }
        }
    }

    toolBar: ToolBar{
        RowLayout{
            anchors.fill: parent

            ToolButton { action: fileNewAction }
            ToolButton { action: fileOpenAction }
            ToolButton { action: fileSaveAction }

            ToolBarSeparator {}

            ToolButton { action: copyAction }
            ToolButton { action: cutAction }
            ToolButton { action: pasteAction }

            ToolBarSeparator {}
            Item { Layout.fillWidth: true }
        }
    }

    statusBar: StatusBar{
        RowLayout{
            anchors.fill: parent
            Label { text: scene.prompt }
            Label { text: scene.pos }
        }
    }
    ColumnLayout{
        anchors.fill: parent
        ToolBar{
            id: quinceToolBar
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            ColumnLayout{
                ToolButton { action: selectAction }
                ToolButton { action: zoomAction }
                ToolButton { action: createSegmentAction }
                ToolButton { action: createRectangleAction }
                ToolButton { action: createPolygonAction }
                ToolButton { action: createPointAction }
                ToolButton { action: createCurveAction }
                ToolButton { action: createCircleAction }
                ToolButton { action: createArcAction }
                Item { Layout.fillHeight: true}
            }
        }

        Rectangle{
            id: scene
            anchors.left: quinceToolBar.right
            width: parent.width - quinceToolBar.width
            height: parent.height


            transform: Scale{ xScale: scene.scale; yScale: scene.scale}

            color: "black"
            property int shape: ShapeType.None
            property real scale: 1.0
            property string pos
            property string prompt

            Component{
                id: lineFactory
                LineItem{
                }
            }

            Component{
                id: rectFactory
                RectItem{
                }
            }

            Component{
                id: circleFactory
                CircleItem{
                }
            }

            Component{
                id: bezierFactory
                BezierCurveItem{
                }
            }

            Component{
                id: pointFactory
                PointItem{
                    id: point
                }
            }

            LineItem{
                id: tempLine
            }

            RectItem{
                id: tempRect
            }

            CircleItem{
                id: tempCircle
            }

            BezierCurveItem{
                id: tempBezier
            }

            MouseArea {
                anchors.fill: parent
                cursorShape: scene.shape == ShapeType.None ? Qt.ArrowCursor : Qt.CrossCursor
                property bool isFirstClick: false
                property bool isSecondClick: false
                property bool isThirdClick: false
                property point tempPoint

                onPressed: {
                    if (mouse.button == Qt.LeftButton)
                    {
                        if(!isFirstClick && !isSecondClick && !isThirdClick)
                        {
                            switch(scene.shape)
                            {
                            case ShapeType.None:
                                mouse.accepted = false
                                break
                            case ShapeType.Zoom:
                                tempPoint = Qt.point(mouse.x, mouse.y)
                                break
                            case ShapeType.Line:
                                tempLine.p1 = Qt.point(mouse.x, mouse.y)
                                tempLine.p2 = Qt.point(mouse.x, mouse.y)
                                scene.prompt = "Press the button and pecify the end point"
                                break
                            case ShapeType.Rect:
                                tempRect.p1 = Qt.point(mouse.x, mouse.y)
                                tempRect.p2 = Qt.point(mouse.x, mouse.y)
                                scene.prompt = "Press the button and pecify the diagonal point"
                                break
                            case ShapeType.Circle:
                                tempCircle.center = Qt.point(mouse.x, mouse.y)
                                scene.prompt = "Press the button and specify one of the point on the circle"
                                break
                            case ShapeType.Bezier:
                                tempBezier.p1 = Qt.point(mouse.x, mouse.y)
                                tempBezier.p2 = Qt.point(mouse.x, mouse.y)
                                tempBezier.p3 = Qt.point(mouse.x, mouse.y)
                                scene.prompt = "Press the button and specify the end point of the Bezier"
                                break
                            }
                            isFirstClick = true
                        }
                    }
                }

                onReleased: {
                    if (mouse.button == Qt.LeftButton)
                    {
                        if(isFirstClick && !isSecondClick && !isThirdClick)
                        {
                            switch(scene.shape)
                            {
                            case ShapeType.Line:
                                lineFactory.createObject(parent, { "p1": tempLine.p1, "p2": Qt.point(mouse.x, mouse.y)})
                                isFirstClick = false
                                isSecondClick = false
                                scene.prompt = "A line created, click to start a new line"
                                break
                            case ShapeType.Rect:
                                rectFactory.createObject(parent, { "p1": tempRect.p1, "p2": Qt.point(mouse.x, mouse.y)})
                                isFirstClick = false
                                isSecondClick = false
                                scene.prompt = "A Rectangle created, click to start a new Rectangle"
                                break
                            case ShapeType.Bezier:
                                tempBezier.p2 = Qt.point(mouse.x, mouse.y)
                                tempBezier.p3 = Qt.point(mouse.x, mouse.y)
                                isSecondClick = true
                                scene.prompt = "Press and specify the control point of the Bezier"
                                break
                            case ShapeType.Circle:
                                var end = Qt.vector2d(mouse.x, mouse.y)
                                var cen = tempCircle.center
                                var st = Qt.vector2d(cen.x, cen.y)
                                var vec = end.minus(st)
                                circleFactory.createObject(parent, {"center": tempCircle.center, "radius": vec.length()})
                                isFirstClick = false
                                isSecondClick = false
                                scene.prompt = "A circle created, click to start a new circle"
                                break

                            case ShapeType.Point:
                                pointFactory.createObject(parent, {"pos": Qt.point(mouse.x, mouse.y)})
                                isFirstClick = false
                                break
                            case ShapeType.Zoom:
                                scene.scale *= (mouse.x - tempPoint.x > 0 ? 1.25 : 0.8)
                                break
                            }
                        }
                        else if(isFirstClick && isSecondClick && !isThirdClick)
                        {
                            if(scene.shape === ShapeType.Bezier)
                            {
                                tempBezier.p2 = Qt.point(mouse.x, mouse.y)
                                bezierFactory.createObject(parent, {"p1": tempBezier.p1, "p2": Qt.point(mouse.x, mouse.y), "p3": tempBezier.p3})
                                isFirstClick = false
                                isSecondClick = false
                                isThirdClick = false
                                scene.prompt = "A bezier curve created, click to start a new bezier"
                            }
                        }
                    }
                }

                onPositionChanged:{
                    if(isFirstClick && !isSecondClick && !isThirdClick)
                    {
                        switch(scene.shape)
                        {
                        case ShapeType.Zoom:
                            scene.scale *= (mouse.x - tempPoint.x > 0 ? 1.028806543621399 : 0.972)
                            break
                        case ShapeType.Line:
                            tempLine.p2 = Qt.point(mouse.x, mouse.y)
                            break
                        case ShapeType.Rect:
                            tempRect.p2 = Qt.point(mouse.x, mouse.y)
                            break
                        case ShapeType.Bezier:
                            tempBezier.p3 = Qt.point(mouse.x, mouse.y)
                            break
                        case ShapeType.Circle:
                            var end = Qt.vector2d(mouse.x, mouse.y)
                            var cen = tempCircle.center
                            var st = Qt.vector2d(cen.x, cen.y)
                            var vec = end.minus(st)
                            tempCircle.radius = vec.length()
                            break
                        }
                    }
                    else if(isFirstClick && isSecondClick && !isThirdClick)
                    {
                        if(scene.shape === ShapeType.Bezier)
                        {
                            tempBezier.p2 = Qt.point(mouse.x, mouse.y)
                        }
                    }

                    scene.pos =  qsTr ("Position:  (%1 , %2) ").arg(mouse.x).arg(mouse.y)
                }
            }
        }
    }
}
